﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Disappearwind.PortalSolution.PortalWeb.Models;

namespace Disappearwind.PortalSolution.PortalWeb.Business
{
    /// <summary>
    /// Business for PortalDocument
    /// </summary>
    public class PortalDocumentBusiness : BaseBusiness
    { /// <summary>
        /// Get PortalDocument by menuId, will return all PortalDocuments in the PortalMenu
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public List<PortalDocument> GetPortalDocumentList(int menuId)
        {
            var c = from p in DBContext.PortalDocument
                    where p.PortalMenu.Id == menuId
                    select p;
            if (c == null || c.Count() == 0)
            {
                return new List<PortalDocument>();
            }
            else
            {
                return c.ToList();
            }
        }
        /// <summary>
        /// Get all document of current menu by keyword
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<PortalDocument> GetPortalDocumentList(string keyword)
        {
            var c = from p in DBContext.PortalMenu
                    where p.Keyword == keyword
                    select p;
            if (c != null && c.Count() > 0)
            {
                var r = from p in DBContext.PortalDocument
                        where p.PortalMenu == c.FirstOrDefault() && p.State
                        select p;
                return r.ToList();
            }
            else
            {
                return new List<PortalDocument>();
            }
        }
        /// <summary>
        /// Get PortalDocument by documentId,if not exist return a empty PortalDocument object
        /// </summary>
        /// <param name="documentId"></param>
        /// <returns></returns>
        public PortalDocument GetPortalDocument(int documentId)
        {
            var c = from p in DBContext.PortalDocument
                    where p.Id == documentId
                    select p;
            if (c != null && c.Count() > 0)
            {
                return c.FirstOrDefault();
            }
            else
            {
                return new PortalDocument();
            }
        }
        /// <summary>
        /// Add a new PortalDocument to database
        /// </summary>
        /// <param name="portalDocument"></param>
        /// <returns></returns>
        public bool AddPortalDocument(PortalDocument portalDocument)
        {
            if (ValidatePortalDocument(portalDocument))
            {
                DBContext.AddToPortalDocument(portalDocument);
                DBContext.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Update a PortalDocument
        /// </summary>
        /// <param name="portalDocument"></param>
        /// <returns></returns>
        public bool UpdatePortalDocument(PortalDocument portalDocument)
        {
            portalDocument.CreateDate = Convert.ToDateTime(portalDocument.CreateDate);
            if (ValidatePortalDocument(portalDocument))
            {
                PortalDocument originalPortalDocument = GetPortalDocument(portalDocument.Id);
                DBContext.ApplyPropertyChanges(originalPortalDocument.EntityKey.EntitySetName, portalDocument);
                DBContext.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Delete a PortalDocument
        /// </summary>
        /// <param name="documentId"></param>
        public void DeletePortalDocument(int documentId)
        {
            PortalDocument originalPortalDocument = GetPortalDocument(documentId);
            DBContext.DeleteObject(originalPortalDocument);
            DBContext.SaveChanges();
        }
        /// <summary>
        /// Validate the PortalDocument
        /// </summary>
        /// <param name="portalDocument"></param>
        /// <returns></returns>
        public bool ValidatePortalDocument(PortalDocument portalDocument)
        {
            CleanErrorDic();
            if (string.IsNullOrEmpty(portalDocument.Name))
            {
                ModelStateDic.AddModelError("Name", "Name should not be empty.");
            }
            return ModelStateDic.IsValid;
        }
    }
}
